﻿using System;

namespace CSEngine
{
    public sealed class GameBoard
    {
        private byte rows, cols;
        private GameBoardSpace[,] board;

        public GameBoard()
        {
            rows = 15;
            cols = 15;
            reset();
        }

        public GameBoard(byte length, byte width)
        {
            rows = length;
            cols = width;
            reset();
        }

        public static GameBoard DeepCopy(GameBoard original)
        {
            GameBoard result = new GameBoard(original.Rows,original.Cols);
            result.board = new GameBoard.GameBoardSpace[original.Rows, original.Cols];
            for (byte x = 1; x <= result.Rows; x++)
            {
                for (byte y = 1; y <= result.Cols; y++)
                {
                    HexDigit hx = (HexDigit)x;
                    HexDigit hy = (HexDigit)y;
                    result.board[x - 1, y - 1] = new GameBoardSpace();
                    result.MarkSpace(hx, hy, original.GetSpace(hx, hy).Color);
                }
            }

            return result;
        }

        public byte Rows
        {
            get { return rows; }
        }

        public byte Cols
        {
            get { return cols; }
        }

        public void reset()
        {
            board = new GameBoardSpace[Rows,Cols];
            for (byte x = 0; x < Rows; x++)
            {
                for (byte y = 0; y < Cols; y++)
                {
                    board[x, y] = new GameBoardSpace();
                }
            }
        }

        public override string ToString()
        {
            string result = " ";
            byte x, y;
            for (x = 1; x <= Cols; x++)
                result += " " + HexDigit.FromByte(x);

            for (x = 1; x <= Rows; x++)
            {
                result += "\n" + (HexDigit)x + " ";
                for (y = 1; y <= Cols; y++)
                    result += GetSpace((HexDigit)x, (HexDigit)y).ToString() + " ";
            }

            result += '\n';

            return result;
        }

        public void MarkSpace(Move m, GameBoardSpace.SpaceColor color)
        {
            MarkSpace(m.x, m.y, color);
        }

        public void MarkSpace(HexDigit x, HexDigit y, GameBoardSpace.SpaceColor color)
        {
            board[x - 1, y - 1].Color = color;
        }

        public GameBoardSpace GetSpace(Move m)
        {
            return GetSpace(m.x, m.y);
        }

        public GameBoardSpace GetSpace(HexDigit x, HexDigit y)
        {
            return board[x-1, y-1];
        }

        public class GameBoardSpace
        {
            public enum SpaceColor { BLACK = -1, EMPTY = 0, WHITE = 1 }
            private SpaceColor color;

            public GameBoardSpace()
            { color = SpaceColor.EMPTY; }

            public SpaceColor Color
            {
                get { return color; }
                set { color = value; }
            }

            public override string ToString()
            {
                switch (Color)
                {
                    case SpaceColor.BLACK:
                        return "@";
                    case SpaceColor.WHITE:
                        return "O";
                    case SpaceColor.EMPTY:
                        return "X";
                    default:
                        return base.ToString();
                }
            }
        }
    }
}
